const fs=require('fs')
const path=require('path')
const puppeteer = require('puppeteer');
const {verification_code}=require('./api')
const {appendFile,close_fn}=require('./tools')
const user=JSON.parse(fs.readFileSync('./users.json').toString())

~(async function(){
    for (let index = 0; index < 20; index++) {
        const browser=await puppeteer.launch({ 
            headless: false,
            devtools: false,
            defaultViewport:{
                width:1920,
                height:800
            },
            ignoreDefaultArgs: ["--enable-automation"],
            args: ['--no-sandbox', '--disable-setuid-sandbox']
        })
        const context = await browser.createIncognitoBrowserContext();
        const page=await context.newPage();
        page.on('error',function(){
            console.log(user[index].name+'发生错误');
            appendFile(path.resolve('./other.txt'),user[index].name+'###'+user[index].pwd+'\r\n')
            browser.close()
        })
        await page.goto('http://cpquery.cnipa.gov.cn',{ timeout:60000, waitUntil:'networkidle0'})
        login(context,page,user[index])
    }
})()



async function login(browser,page,user){
    await page.type('#username', user.name)
    await page.type('#password', user.pwd)    
    await page.waitFor(500)
    await page.hover('#div1')
    await page.waitFor(1500)    
    const X=530,Y=229
    const base64=await page.screenshot({
        encoding:'base64',
        clip:{
            x:X,
            y:Y,
            width:305,
            height:248
        }
    })
    let res= await verification_code({
        user: 'St1nger',
        pass: 'IVERSON3399',
        softid:'901671', 
        codetype: '9103',  
        file_base64: base64
    })
    if(!res.err_no){
        let arr=res.pic_str.split('|')
        arr.forEach(async el=>{
            let pos=el.split(',')
            await page.mouse.click(+pos[0]+X,+pos[1]+Y)
            await page.waitFor(300) 
        })
        
    }else{
        console.log('验证码获取错误');
    }
    await page.waitFor(800)
    if(await page.$eval('#selectyzm_text_dz',el => el.innerHTML)==='验证成功'){
        await page.evaluate(()=>{
            login('false','2','2');
        })
        // await page.waitForNavigation()
        await page.waitFor(2000)
        if(await page.$('#OpenDialog_0')){
            const title=await page.$eval('#OpenDialog_0 .top-title',el => el.innerHTML)
            if(title=='密码提示'){
                await appendFile(path.resolve('./change_pwd.txt'),user.name+'###'+user.pwd+'\r\n')
            }else if(title=="登录用户达到上限"){
                await appendFile(path.resolve('./success.txt'),user.name+'###'+user.pwd+'\r\n')
            }

        }else if(await page.$('#attention_token')){
            await appendFile(path.resolve('./success.txt'),user.name+'###'+user.pwd+'\r\n')
        }else{
            await appendFile(path.resolve('./other.txt'),user.name+'###'+user.pwd+'\r\n')
        }
        browser.close()
    }else{
        await page.reload()
        await login(browser,page,user)
    }
}


